﻿using AutoMapper;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.ProcessEgine.API.Applocations.Commands.FormTypeInfoCommand;
using B.S.ProcessEgine.Domain.Entities;
using B.S.ProcessEgine.ErrorCode;
using B.S.ProcessEgine.Infractructure.Interfaces;
using MediatR;

namespace B.S.ProcessEgine.API.Applocations.CommandHandlers.FormTypeInfoHandler
{
    public class DelFormTypeInfoHandler:IRequestHandler<DelFromTypeInfoCommand,ApiResult>
    {
        IFormTypeRepository formtypeinforepository;
        ILogger<DelFormTypeInfoHandler> logger;
        IMapper mapper;

        public DelFormTypeInfoHandler(IFormTypeRepository formtypeinforepository, ILogger<DelFormTypeInfoHandler> logger, IMapper mapper)
        {
            this.formtypeinforepository = formtypeinforepository;
            this.logger = logger;
            this.mapper = mapper;
        }
        /// <summary>
        /// 表单删除
        /// </summary>
        /// <param name="request">表单id</param>
        /// <param name="cancellationToken"></param>
        /// <returns>返回受影响行数</returns>
        public async Task<ApiResult> Handle(DelFromTypeInfoCommand request, CancellationToken cancellationToken)
        {
            try
            {
                int c = 0;

                foreach (var item in request.FormTypeInfoId)
                {
                    var list = await formtypeinforepository.GetAsync(d => d.TypeNo.Equals(item));
                    list.IsDel = true;
                    await formtypeinforepository.UpdateAsync(list);
                    c++;
                }


                return c > 0 ? ApiResult.Success(StateCodeEnum.Ok) : ApiResult.Fail("删除失败", StateCodeEnum.Fail);
            }
            catch (Exception ex)
            {
                logger.LogError("删除表单出错" + ex.Message);
                throw;
            }
        }
    }
}
